// --------------------------------------------------------------------------------------------------------------------
// <copyright file="InteractionManifest.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Test.Robotics.Interaction
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Robotics.Interaction.Runtime.Input;
    using Microsoft.Robotics.Interaction.Visualization;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Visualization;

    /// <summary>
    /// Provides methods for creating manifests in a type-checked way.
    /// </summary>
    public static class InteractionManifest
    {
        /// <summary>
        /// Adds the gamepad input agent 
        /// </summary>
        /// <param name="manifest">The manifest to add to</param>
        /// <param name="views">The set of views to add any visualization to.</param>
        /// <returns>The gamepad agent</returns>
        public static GamePadAgent AddGamePadAgent(Manifest manifest, List<ViewDefinition> views)
        {
            TimerAgent timer = new TimerAgent(
                name: "GamePadTimer",
                timerInterval: 25);
            manifest.Agents.Add(timer);

            GamePadAgent gamepad = new GamePadAgent(
                name: "GamePadAgent",
                producer: timer);
            manifest.Agents.Add(gamepad);

            return gamepad;
        }

        /// <summary>
        /// Adds the gamepad emulator agent and associated view
        /// </summary>
        /// <param name="manifest">The manifest to add to</param>
        /// <param name="views">The set of views to add any visualization to.</param>
        /// <returns>The gamepad agent</returns>
        public static GamePadEmulatorAgent AddGamePadEmulatorAgent(Manifest manifest, List<ViewDefinition> views)
        {
            TimerAgent timer = new TimerAgent(
                name: "GamePadTimer",
                timerInterval: 25);
            manifest.Agents.Add(timer);

            GamePadEmulatorAgent gamepadEmulator = new GamePadEmulatorAgent(
                name: "GamePadAgent",
                producer: timer);
            manifest.Agents.Add(gamepadEmulator);

            GamePadEmulatorModelAgent gamepadEmulatorModelAgent = new GamePadEmulatorModelAgent("GamePadEmulatorModel", gamepadEmulator, 25);
            manifest.Agents.Add(gamepadEmulatorModelAgent);

            ViewDefinition gamepadView = new ViewDefinition(
                name: "GamePadEmulator",
                viewType: typeof(GamePadEmulatorView),
                modelAgent: gamepadEmulatorModelAgent);

            views.Add(gamepadView);

            return gamepadEmulator;
        }
        
        /// <summary>
        /// Adds a proxy for a remote game pad agent.
        /// </summary>
        /// <param name="manifest">The manifest to add to.</param>
        /// <param name="views">The set of views to add to</param>
        /// <param name="sourceNameOrAddress">The name or IP address of the game pad machine</param>
        /// <returns>The game pad proxy</returns>
        public static IProducer<GamePadAgentMessage> AddRemoteGamePadAgentProxy(Manifest manifest, List<ViewDefinition> views, string sourceNameOrAddress)
        {
            ProxyDefinition remoteProxy = ProxyDefinition.CreateRemoteProxy(
                name: "GamePad",
                address: sourceNameOrAddress,
                publishContracts: new[] { typeof(GamePadAgentMessage) },
                publishNetworkPortNumber: (int)RemotingPorts.GamePadPublish,
                controlContracts: null,
                controlNetworkPortNumber: 0);
            manifest.RemoteAgentProxies.Add(remoteProxy);

            return remoteProxy.AsProducer<GamePadAgentMessage>();
        }

        /// <summary>
        /// Adds a proxy for a remote game pad agent.
        /// </summary>
        /// <param name="manifest">The manifest to add to.</param>
        /// <param name="views">The set of views to add to</param>
        /// <param name="gamePadProducer">The producer of game pad messages</param>
        /// <param name="targetNameOrAddress">The name or IP address of the game pad machine</param>
        /// <returns>The game pad proxy definition</returns>
        public static ProxyDefinition AddNetworkEnabledGamePadAgentProxy(Manifest manifest, List<ViewDefinition> views, IProducer<GamePadAgentMessage> gamePadProducer, string targetNameOrAddress)
        {
            ProxyDefinition gamePadProxy = ProxyDefinition.CreateNetworkEnabledProxy(
                name: gamePadProducer.Name + "Proxy",
                producer: gamePadProducer.Name,
                address: targetNameOrAddress,
                publishContracts: new[] { typeof(GamePadAgentMessage) },
                publishNetworkPortNumber: (int)RemotingPorts.GamePadPublish,
                controlContracts: null,
                controlNetworkPortNumber: 0);
            manifest.NetworkEnabledAgentProxies.Add(gamePadProxy);

            return gamePadProxy;
        }
        
        /// <summary>
        /// Adds a file recorder proxy for the gamepad agent to the specified manifest.
        /// </summary>
        /// <param name="manifest">The manifest to add to.</param>
        /// <param name="gamepad">The agent to record.</param>
        /// <returns>A file recorder proxy</returns>
        public static FileRecorderProxy AddGamePadRecorder(Manifest manifest, IProducer<GamePadAgentMessage> gamepad)
        {
            FileRecorderProxy gamepadRecorder = new FileRecorderProxy(
                name: gamepad.Name + "Recorder",
                filePath: null,
                messageTypes: new[] { typeof(GamePadAgentMessage) },
                producer: gamepad.Name);
            manifest.Agents.Add(gamepadRecorder);

            return gamepadRecorder;
        }

        /// <summary>
        /// Adds a file player for gamepad input agent recordings
        /// </summary>
        /// <param name="manifest">The manifest to add to</param>
        /// <param name="views">The set of views to add any visualization to.</param>
        /// <param name="timer">The timer driving the playback</param>
        /// <param name="directory">The directory containing the recorded file</param>
        /// <param name="fileName">Optional. The name of recording file </param>
        /// <returns>The gamepad file player</returns>
        public static FilePlayerProxy AddGamePadPlayback(Manifest manifest, List<ViewDefinition> views, TimerAgent timer, string directory, string fileName = "GamePadAgent.xml")
        {
            // add a proxy with the same name
            FilePlayerProxy playbackProxy = new FilePlayerProxy(
                name: "GamePadAgent",
                filePath: Path.Combine(directory, fileName),
                messageTypes: new[] { typeof(GamePadAgentMessage) },
                timerAgent: timer);
            manifest.Agents.Add(playbackProxy);

            // return the fake agent, so that other manifest-creation functions get the right type
            // since the only things used from the fake agent are the IProducer interfaces and the agent name, this is ok
            return playbackProxy;
        }

        /// <summary>
        /// Creates a simple manifest that just remotes gamepad messages to the target computer
        /// </summary>
        /// <param name="targetNameOrAddress">The name or IP address of the target computer</param>
        /// <returns>The manifest</returns>
        public static Manifest CreateGamePadRemotePublishingManifest(string targetNameOrAddress)
        {
            Manifest manifest = new Manifest();
            var views = new List<ViewDefinition>();

            GamePadAgent gamePad = AddGamePadAgent(manifest, views);
            manifest.ActiveAgents.Add(gamePad.Name);
            ProxyDefinition gamePadProxy = AddNetworkEnabledGamePadAgentProxy(manifest, views, gamePad, targetNameOrAddress);
            manifest.ActiveAgents.Add(gamePadProxy.Name);

            return manifest;
        }

        /// <summary>
        /// Creates a simple manifest that just remotes gamepad messages from a game pad emulator the target computer
        /// </summary>
        /// <param name="targetNameOrAddress">The name or IP address of the target computer</param>
        /// <returns>The manifest</returns>
        public static Manifest CreateGameEmulatorPadRemotePublishingManifest(string targetNameOrAddress)
        {
            Manifest manifest = new Manifest();
            var views = new List<ViewDefinition>();

            GamePadEmulatorAgent gamePad = AddGamePadEmulatorAgent(manifest, views);
            manifest.ActiveAgents.Add(gamePad.Name);
            ProxyDefinition gamePadProxy = AddNetworkEnabledGamePadAgentProxy(manifest, views, gamePad, targetNameOrAddress);
            manifest.ActiveAgents.Add(gamePadProxy.Name);

            UserInterfaceAgent userInterface = new UserInterfaceAgent("UserInterface", views.ToArray());
            manifest.Agents.Add(userInterface);
            manifest.ActiveAgents.Add(userInterface.Name);

            return manifest;
        }    
    }
}
